package com.oamicnet.mapper;

import com.oamicnet.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StudentMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Student> findAll() {
        String sql = "select id,name,email from t_student";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
    }

    public void save(Student student) {
        String sql = "insert into t_student(name,email) values(?,?)";
        jdbcTemplate.update(sql, student.getName(), student.getEmail());
    }

    public void delete(Integer id) {
        String sql = "delete from t_student where id=?";
        jdbcTemplate.update(sql, id);
    }

    public void update(Student student) {
        String sql = "update t_student set name=?,email=? where id=?";
        jdbcTemplate.update(sql, student.getName(), student.getEmail(), student.getId());
    }

    public Student findById(Integer id) {
        String sql = "select id,name,email from t_student where id=?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Student.class), id);
    }
}
